package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.AttendCourse;
import cn.sunlelin.educationserver.mapper.AttendCourseMapper;
import cn.sunlelin.educationserver.service.AttendCourseService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class AttendCourseController {
    @Autowired
    private AttendCourseService attendCourseService;

    @PostMapping("/user/attendCourse/createAttendCourse")
    public Map<String,Object> createAttendCourse(@RequestBody AttendCourse attendCourse){
        Map<String,Object> map=new HashMap<>();
        System.out.println(attendCourse);
        if(attendCourseService.createAttendCourse(attendCourse)){
            map.put("code",200);
            map.put("msg","用户参与课程成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","用户已参与该课程");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/user/attendCourse/updateAttendCourse")
    public Map<String,Object> updateAttendCourse(@RequestBody AttendCourse attendCourse){
        Map<String,Object> map=new HashMap<>();
        if(attendCourseService.updateAttendCourse(attendCourse)){
            map.put("code",200);
            map.put("msg","修改用户参与课程情况成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","用户未参与此课程");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/user/attendCourse/selectAttendCourse")
    public Map<String,Object> selectAttendCourse(@RequestBody AttendCourse attendCourse){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> attendCourseOut = attendCourseService.selectAttendCourse(attendCourse);
        if(attendCourseOut!=null){
            map.put("code",200);
            map.put("msg","查询用户参与该课程情况成功");
            map.put("dataobject",attendCourseOut);
        }
        else {
            map.put("code",500);
            map.put("msg","查询用户参与该课程情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/user/attendCourse/selectAttendCourseAndName")
    public Map<String,Object> selectAttendCourseAndName(@RequestBody AttendCourse attendCourse){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> attendCourseOut = attendCourseService.selectAttendCourseAndName(attendCourse);
        if(attendCourseOut!=null){
            map.put("code",200);
            map.put("msg","查询用户参与该课程情况成功");
            map.put("dataobject",attendCourseOut);
        }
        else {
            map.put("code",500);
            map.put("msg","查询用户参与该课程情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/user/attendCourse/selectAttendByUserId/{userId}")
    public Map<String,Object> selectAttendByUserId(@PathVariable Integer userId){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> attendList= attendCourseService.selectAttendByUserId(userId);
        if(attendList.size()!=0){
            map.put("code",200);
            map.put("msg","查询用户参与课程成功");
            map.put("dataobject",attendList);
        }
        else {
            map.put("code",500);
            map.put("msg","查询用户参与课程失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/attendCourse/getPageCourseByUserId/{currentPage}/{pageSize}/{userId}")
    public Map<String,Object> getPageCourseByUserId(@PathVariable Integer currentPage,
                                                    @PathVariable Integer pageSize,
                                                    @PathVariable Integer userId){
        if(currentPage==null) currentPage=1;
        if(pageSize==null)pageSize=2;
        Page<Map<String,Object>> page = attendCourseService.getPageCourseByUserId(currentPage,pageSize,userId);
        List<Map<String,Object>> pageList=page.getResult();
        long total = page.getTotal();
        Map<String,Object> m=new HashMap<>();
        m.put("pageList",pageList);
        m.put("total",total);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","UserId分页查询课程信息成功");
        map.put("dataobject",m);
        return map;
    }

    @PostMapping("/user/attendCourse/deleteAttendCourse")
    public Map<String,Object> deleteAttendCourse(@RequestBody AttendCourse attendCourse){
        Map<String,Object> map=new HashMap<>();
        if(attendCourseService.deleteAttendCourse(attendCourse)){
            map.put("code",200);
            map.put("msg","用户退出课程成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","用户未参与此课程");
            map.put("dataobject",null);
        }
        return map;
    }
}
